package com.cloud.common;

import com.cloud.common.enums.ResultEnumCode;
import lombok.Data;

/**
 * packageName com.cloud.common
 *
 * @author yanshuanglong
 * @version JDK 8
 * @className Result
 * @date 2025/3/20
 * @description TODO
 */
@Data
public class Result<T> {
    private int code;
    private String msg;
    private T data;


    /**
    * AS2
     * @param body
     * @param resultEnumCode
    * @author yangshuanglong
    * @date 2025/3/20
    * {@link Result<T>}
    * return Result<T>
    * @description 如果创建实例时传入的body是空就为其设置值
    */
    public static <T> Result <T> custom(T body, ResultEnumCode resultEnumCode) {
        Result<T> custom = custom(body);
        custom.setCode(resultEnumCode.getCode());
        custom.setMsg(resultEnumCode.getMsg());
        return custom;
    }

    /**
    * AS2
     * @param body
    * @author yangshuanglong
    * @date 2025/3/20
    * {@link Result<T>}
    * return Result<T>
    * @description 负责创建Result 实例
    */
    public static <T> Result <T> custom(T body) {
        Result<T> result = new Result<T>();
        if (body != null)
            result.setData(body);
        return result;
    }

    /**
    * AS2
     * @param data
    * @author yangshuanglong
    * @date 2025/3/20
    * {@link Result}
    * return Result
    * @description 返回成功实例
    */
    public static <T> Result ok(T data) {
        return custom(data, ResultEnumCode.SUCCESS);
    }

    /**
    * AS2
     * @param
    * @author yangshuanglong
    * @date 2025/3/20
    * {@link Result}
    * return Result
    * @description 当不传参数时返回默认实例
    */
    public static <T> Result ok() {
        return Result.ok(null);
    }

    public static <T> Result error(T data) {

        return custom(data, ResultEnumCode.FAIL);
    }

    public static <T> Result error() {
        return error(null);
    }

}
